package com.bjpowernode.rbac.web.controller;

import com.bjpowernode.rbac.anno.RequestPermission;
import com.bjpowernode.rbac.entity.*;
import com.bjpowernode.rbac.query.CustomerQuery;
import com.bjpowernode.rbac.query.EchartsQuery;
import com.bjpowernode.rbac.service.*;
import com.bjpowernode.rbac.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author jy
 * @version 1.0
 * @date 2022/2/7 19:13
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private SystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private CustomerTraceHistoryService customerTraceHistoryService;

    @Autowired
    private CustomerTransferService customerTransferService;


    @RequestMapping("/list")
    @RequestPermission({"客户列表权限","customer:list"})
    public String list(Model model, @ModelAttribute("qo") CustomerQuery customerQuery){
        customerQuery.setPageSize(0);
        //查询营销专员
        List<Employee> sellers = employeeService.selectAllSellers();
        model.addAttribute("sellers",sellers);
        customerQuery.setPageSize(2);
        //查询所有用户信息
        PageInfo<Customer> pageInfo = customerService.selectCustomerByCondition(customerQuery);

        //查询当前登录员工
        Employee employee = UserContext.getEmployee();
        //...
        //传到页面
        model.addAttribute("pageInfo",pageInfo);

        //查询添加用户模态窗里面的职业和来源
        PageInfo<SystemDictionaryItem> pageInfoJob = systemDictionaryItemService.selectJob();
        model.addAttribute("pageInfoJob",pageInfoJob.getList());
        //查询添加用户模态窗里面的来源
        PageInfo<SystemDictionaryItem> pageInfoSource = systemDictionaryItemService.selectSource();
        model.addAttribute("pageInfoSource",pageInfoSource.getList());
        //查询用户已有的跟进方式
        PageInfo<SystemDictionaryItem> pageInfoTraceType = customerTraceHistoryService.selectTraceType();
        model.addAttribute("pageInfoTraceType",pageInfoTraceType.getList());
        //查询用户已有的跟进结果
        PageInfo<CustomerTraceHistory> pageInfoTraceResult = customerTraceHistoryService.selectTraceResult();
        model.addAttribute("pageInfoTraceResult",pageInfoTraceResult.getList());
        //查询用户已有的跟进类型
        PageInfo<CustomerTraceHistory> pageInfoType = customerTraceHistoryService.selectType();
        model.addAttribute("pageInfoType",pageInfoType.getList());
        //查询编辑列表里面的新旧营销人员
        PageInfo<CustomerTransfer> pageInfoCustomerTransfer = customerTransferService.selectOldOrNewSeller();
        model.addAttribute("pageInfoCustomerTransfer",pageInfoCustomerTransfer.getList());
        //跳转到页面
        return "customer/list";
    }



    /**
     * 潜在客户管理
     * @param model
     * @param customerQuery
     * @return
     */
    @RequestMapping("/potentialCustomer")
    @RequestPermission({"潜在客户列表权限","customer:potentialCustomer"})
    public String potentialCustomer(Model model, @ModelAttribute("qo") CustomerQuery customerQuery){
        customerQuery.setPageSize(0);
        //查询市场专员
        List<Employee> sellers = employeeService.selectAllSellers();
        model.addAttribute("sellers",sellers);
        customerQuery.setPageSize(2);
        //查询潜在客户信息
        PageInfo<Customer> pageInfo = customerService.selectPotentialCustomerByCondition(customerQuery);
        //传到页面
        model.addAttribute("pageInfo",pageInfo);
        //查询职业下拉框
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectAllJobs();
        model.addAttribute("jobs",jobs);
        //查询来源下拉框
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectAllSources();
        model.addAttribute("sources",sources);
        //查询跟进方式下拉框
        List<SystemDictionaryItem> traceTypeIds = systemDictionaryItemService.selectAllTraceTypeIds();
        model.addAttribute("traceTypeIds",traceTypeIds);
        //查询跟进结果下拉框
        List<SystemDictionaryItem> traceResults = systemDictionaryItemService.selectAllTraceResults();
        model.addAttribute("traceResults",traceResults);
        //查找移交里面的新营销人员
        List<Employee> employees = employeeService.selectAllNewSellers();
        model.addAttribute("employees",employees);
        //查询跟进类型下拉框
        List<SystemDictionaryItem> types = systemDictionaryItemService.selectAllTypes();
        model.addAttribute("types",types);
        //跳转到页面
        return "customer/potentialcustomerlist";
    }



    /**
     * 客户池管理(未完善)
     * @param model
     * @param customerQuery
     * @return
     */
    @RequestMapping("/customerPool")
    @RequestPermission({"客户池列表权限","customer:customerPool"})
    public String customerPool(Model model, @ModelAttribute("qo") CustomerQuery customerQuery){

        customerQuery.setPageSize(0);
        //查询营销专员
        List<Employee> sellers = employeeService.selectAllSellers();
        model.addAttribute("sellers",sellers);
        customerQuery.setPageSize(2);

        //查询客户池信息
        PageInfo<Customer> pageInfo = customerService.selectCustomerPoolByCondition(customerQuery);
        //传到页面
        model.addAttribute("pageInfo",pageInfo);

        //查找移交里面的新营销人员
        List<Employee> employees = employeeService.selectAllNewSellers();
        model.addAttribute("employees",employees);

        //跳转到页面
        return "customer/customerpoollist";
    }



    /**
     * 失败客户管理
     * @param model
     * @param customerQuery
     * @return
     */
    @RequestMapping("/failCustomer")
    @RequestPermission({"失败客户列表权限","customer:failCustomer"})
    public String failCustomer(Model model, @ModelAttribute("qo") CustomerQuery customerQuery){

        customerQuery.setPageSize(0);
        //查询营销专员
        List<Employee> sellers = employeeService.selectAllSellers();
        model.addAttribute("sellers",sellers);
        customerQuery.setPageSize(2);


        //调用业务层获取数据
        PageInfo<Customer> pageInfo = customerService.selectFailCustomerByCondition(customerQuery);

        //传到页面
        model.addAttribute("pageInfo",pageInfo);
        //跳转到页面
        return "customer/failcustomerlist";
    }

    /**
     * 正式客户管理
     * @param model
     * @param customerQuery
     * @return
     */
    @RequestMapping("/formalCustomer")
    @RequestPermission({"正式客户列表权限","customer:formalCustomer"})
    public String formalCustomer(Model model, @ModelAttribute("qo") CustomerQuery customerQuery){

        customerQuery.setPageSize(0);
        //查询营销专员
        List<Employee> sellers = employeeService.selectAllSellers();
        model.addAttribute("sellers",sellers);
        customerQuery.setPageSize(2);
        //调用业务层获取数据
        PageInfo<Customer> pageInfo = customerService.selectFormalCustomerByCondition(customerQuery);

        //传到页面
        model.addAttribute("pageInfo",pageInfo);
        //跳转到页面
        return "customer/formallist";
    }


    /**
     * 流失客户管理
     * @param model
     * @param customerQuery
     * @return
     */
    @RequestMapping("/loseCustomer")
    @RequestPermission({"流失客户列表权限","customer:loseCustomer"})
    public String loseCustomer(Model model, @ModelAttribute("qo") CustomerQuery customerQuery){

        customerQuery.setPageSize(0);
        //查询营销专员
        List<Employee> sellers = employeeService.selectAllSellers();
        model.addAttribute("sellers",sellers);
        customerQuery.setPageSize(2);

        //调用业务层获取数据
        PageInfo<Customer> pageInfo = customerService.selectLoseCustomerByCondition(customerQuery);

        //传到页面
        model.addAttribute("pageInfo",pageInfo);
        //跳转到页面
        return "customer/losecustomerlist";
    }



    @RequestMapping("/saveOrUpdate")
    @RequestPermission({"员工新增和编辑权限","customer:saveOrUpdate"})
    public String saveOrUpdate(Customer customer,Long inputTimeDate){
        customerService.saveOrUpdate(customer,inputTimeDate);
        return "redirect:/customer/potentialCustomer";
    }

    @RequestMapping("/UpdateCustomerStatus")
    @RequestPermission({"修改客户状态","customer:UpdateCustomerStatus"})
    public String updateCustomerStatus(Long customerId,Integer status){
        customerService.updateCustomerStatus(customerId,status);
        return "redirect:/customer/potentialCustomer";
    }


    @RequestMapping("/followEdit")
    @RequestPermission({"跟进用户编辑权限","customer:followEdit"})
    public String followEdit(CustomerTraceHistory customerTraceHistory,String traceTime){
        customerTraceHistoryService.followCustomerEdit(customerTraceHistory,traceTime);
        return "redirect:/customer/potentialCustomer";
    }

    @RequestMapping("/transfer")
    @RequestPermission({"潜在客户列表的移交权限","customer:transfer"})
    public String transfer(CustomerTransfer customerTransfer){
        //修改移交历史记录(增添)
        customerTraceHistoryService.transfer(customerTransfer);
        //修改用户表里的seller_id(即改后的新营销员作为再次点击的旧营销员)
        customerService.editSellerId(customerTransfer.getCustomerId(),customerTransfer.getNewSellerId());
        return "redirect:/customer/potentialCustomer";
    }

    @RequestMapping("/poolTransfer")
    @RequestPermission({"客户池的移交权限","customer:poolTransfer"})
    public String poolTransfer(CustomerTransfer customerTransfer){
        //修改移交历史记录(增添)
        customerTraceHistoryService.transfer(customerTransfer);
        //修改用户表里的seller_id(即改后的新营销员作为再次点击的旧营销员)
        customerService.editSellerId(customerTransfer.getCustomerId(),customerTransfer.getNewSellerId());
        return "redirect:/customer/customerPool";
    }

    @RequestMapping("/absorb")
    @RequestPermission({"客户池的吸纳权限","customer:absorb"})
    public String absorb(CustomerTransfer customerTransfer){
        //修改用户表里的seller_id(即改后的新营销员作为再次点击的旧营销员)
        Long newSellerId = UserContext.getEmployee().getId();
        customerTransfer.setNewSellerId(newSellerId);
        //修改移交历史记录(增添)
        customerTraceHistoryService.transfer(customerTransfer);
        //客户表的用户id和对应seller_id同步更新
        customerService.editSellerId(customerTransfer.getCustomerId(),customerTransfer.getNewSellerId());
        return "redirect:/customer/customerPool";
    }

    /**
     * 进入潜在客户报表页面(潜在客户报表的查询)
     */
    @RequestMapping("/toReportsPage")
    @RequestPermission({"报表统计查询权限","customer:toReportsPage"})
    public String toReportsPage(Model model,@ModelAttribute("qo") CustomerQuery customerQuery,String keyword,String dateBegin,String dateEnd){
        customerQuery.setPageSize(0);
        PageInfo<EchartsQuery> pageInfo = customerService.getEchartsQueryList(customerQuery,keyword,dateBegin,dateEnd);
        model.addAttribute("pageInfo",pageInfo);
        return "customer/echarts";
    }

    //处理柱状图的ajax数据
    @RequestMapping("/zhuAjax")
    @ResponseBody
    public Map<String,Object> zhuAjax(Integer groupType,String keyword,String dateBegin,String dateEnd){
        return customerService.countCustomerMake(groupType,keyword,dateBegin,dateEnd);
    }

    //处理饼图的ajax数据
    @RequestMapping("/bingAjax")
    @ResponseBody
    public Map<String,Object> bingAjax(Integer groupType,String keyword,String dateBegin,String dateEnd){
        return customerService.countCustomerMake02(groupType,keyword,dateBegin,dateEnd);
    }
}





